//
//  UIView+MDLoadingAnimation.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "UIView+MDLoadingAnimation.h"
#import "THInfiniteHUDView.h"
#include <objc/runtime.h>
static  NSString  const *lm_MDLoadingAnimationKey = @"MDLoadingAnimation";


@implementation UIView (MDLoadingAnimation)

- (void)showMDLodingView{
    THInfiniteHUDView *animation = objc_getAssociatedObject(self, &lm_MDLoadingAnimationKey);
    if(animation == nil){
        animation = [THInfiniteHUDView showHUDAddedTo:self];
        [animation setBackgroundColor:[UIColor whiteColor]];
        objc_setAssociatedObject(self, &lm_MDLoadingAnimationKey, animation,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    if (self.subviews.count > 1) {
        [self bringSubviewToFront:animation];
    }
    [animation showHUDView];
}
- (void)hidenMDLodingView{
    THInfiniteHUDView *animation = objc_getAssociatedObject(self, &lm_MDLoadingAnimationKey);
    if (animation) {
        objc_setAssociatedObject(self, &lm_MDLoadingAnimationKey,nil,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [animation hideHUDView];
    }
}
- (void)setMDLodingViewColor:(UIColor*)color{
    THInfiniteHUDView *animation = objc_getAssociatedObject(self, &lm_MDLoadingAnimationKey);
    if (animation) {
        [animation setBackgroundColor:color];
    }
}
@end
